package com.wyn.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {
    public static final String SING="111";
    /**
     * @explain 生成token
     * @create 2023-07-01 20:28
     * @author linghanwu
     * @param
     * @return
     **/
    public  static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,7);
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->builder.withClaim(k,v));
        return builder.withExpiresAt(instance.getTime()) //设置过期时间
                .sign(Algorithm.HMAC256(SING));
    }
    /**
     * @explain 验证token合法性
     * @create 2023-07-01 21:18
     * @author linghanwu
     * @param
     * @return
     **/
    public static DecodedJWT verify(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        return  verify;
    }
}
